package com.example.rbac.controller.restful;

import com.example.rbac.entity.Page;
import com.example.rbac.entity.Table;
import com.example.rbac.entity.defefult.Result;
import com.example.rbac.entity.ymlConfig;
import com.example.rbac.service.TablesService;
import com.example.rbac.service.impl.TablesServiceImpl;
import com.example.rbac.uitls.ResultUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/CreateCode")
public class CreateCodeRestController {

    @Autowired
    private TablesService tablesService;

    @Value("${spring.datasource.url}")
    private String url;

    @GetMapping("getTables")
    public Result getTables(){
        List<String> list = tablesService.findTables("rbac");
        return ResultUtil.success(list);
    }



    @GetMapping("getPager")
    public Page getPager(Integer pageNum, Integer pageSize)throws JsonProcessingException {
        Page<Table> page = new Page<Table>();
        if(pageNum == null || pageSize == null){
            pageNum = 1;
            pageSize = 2;
        }
        System.out.println(url);
        String[] strs = url.split("/");
        String dataBase = "";
        if (strs[3].contains("?")){
            String a = String.valueOf(strs[3]);
            a = a.substring(0, a.lastIndexOf("?"));
            dataBase = a;
        }else {
            dataBase = strs[3];
        }
        page = tablesService.getPager(pageNum, pageSize, dataBase);
        return page;
    }
}
